/**
 * @fileoverview Library for Tracking userinteractions inside the Gadget
 */
var gadgets = gadgets || {};

/**
 * @class tracking.
 *
 * @description returns a url pointing to a cdn
 * 
 */
gadgets.cache = (function(){
    
    var staticUrl_ = false;
    
    var extractStaticUrl_ = function (specUrl) {
        if (staticUrl_) {
            return staticUrl_;
        } else {            
            return (staticUrl_ = specUrl.substring(0, specUrl.lastIndexOf('/') + 1));
        }
    };
    
    var loadScript_ = function (scriptPath, pathArr, cb) {
        var script  = document.createElement('script');
        script.type = 'text/javascript';
        if ('\v'=='v') {
            script.onreadystatechange = function(e){
                if (this.readyState === "complete" || this.readyState === "loaded") {
                    if (pathArr.length > 0) {
                        var scriptPath = pathArr.shift();
                        loadScript_(scriptPath, pathArr, cb)
                    } else {
                        if (typeof cb == 'string') {
                            if (cb.indexOf('.') === -1) {
                                window[cb]();
                            } else {
                                var cbArr = cb.split('.');
                                var obj = window;
                                while(cbArr.length > 1) {
                                    obj = obj[cbArr.shift()]
                                }
                                obj[cbArr[0]]();
                            }    
                        }
                    }                
                }
            }
            
        } else {
            script.onload = function(){
                if (pathArr.length > 0) {
                    var scriptPath = pathArr.shift();
                    loadScript_(scriptPath, pathArr, cb)
                }
                else {
                    if (typeof cb == 'string') {
                        if (cb.indexOf('.') === -1) {
                            window[cb]();
                        }
                        else {
                            var cbArr = cb.split('.');
                            var obj = window;
                            while (cbArr.length > 1) {
                                obj = obj[cbArr.shift()]
                            }
                            obj[cbArr[0]]();
                            
                        }
                    }
                }
            }
        }
        script.src = scriptPath.substr(0, 4) == "http" ? scriptPath : extractStaticUrl_(gadgets.util.getUrlParameters().url) + scriptPath;
        //console.log(script.src)
        document.body.appendChild(script);
    };

    return {
        getCachedUrl : function (path) {
            return extractStaticUrl_(gadgets.util.getUrlParameters().url) + path;    
        },

        getCachedPath : function () {
            return extractStaticUrl_(gadgets.util.getUrlParameters().url);
        },

        getCachedJs : function (pathArr, cb) {
                var scriptPath = pathArr.shift();
                loadScript_(scriptPath, pathArr, cb)
        },
        
        getCachedCss : function (pathArr) {
            for (var i in pathArr) {               
                var link = document.createElement('link');
                link.type= 'text/css';
                link.rel = 'stylesheet'
                link.href = this.getCachedUrl(pathArr[i]);
                document.getElementsByTagName('head')[0].appendChild(link);
            }
        }   
    };
    
}());
